from wxpyview.ui.component.common.basewidget import *

class MessageDialog(Widget):
    ICON_MAPS = {"exclamation": wx.ICON_EXCLAMATION,
                 "hand": wx.ICON_HAND,
                 "error": wx.ICON_ERROR,
                 "question": wx.ICON_QUESTION,
                 "information": wx.ICON_INFORMATION,
                 "stop": wx.ICON_STOP,
                 "warning": wx.ICON_WARNING}
    
    BUTTON_MAPS = {"ok": wx.OK,
                   "cancel": wx.CANCEL,
                   "yes": wx.YES,
                   "no": wx.NO,
                   "yes_no": wx.YES_NO,
                   "yes_default": wx.YES_DEFAULT,
                   "no_default": wx.NO_DEFAULT}
    
    def _getParentParam(self, meta_widget):
        # TODO: re-implementation
        return None
    
    def _setParams(self, meta_widget):
        dom = meta_widget.getDOM()
        if not dom:
            raise IOException
        
        self.params["parent"] = self._getParentParam(meta_widget)
        self.params["message"] = WidgetLib.getDomAttr(dom, "title", "")
        self.params["caption"] = WidgetLib.getDomAttr(dom, "caption", wx.MessageBoxCaptionStr)
        self.params["style"] = self._setStyle(dom)
        self.params["pos"] = WidgetLib.getPosParam(dom)

    def _setStyle(self, dom):
        icon_param = WidgetLib.getDomAttr(dom, "icon", None)
        btn_params = WidgetLib.getDomAttr(dom, "button", None)
        
        style = 0
        
        if icon_param != None:
            style |= self.ICON_MAPS.get(icon_param, 0)
        
        if btn_params != None:
            btns = filter(None, map(lambda x: x.strip(), btn_params.split(',')))
            btn_styles = map(lambda x: self.BUTTON_MAPS.get(x, 0), btns)
        
            style |= reduce(lambda x, sum: sum|x, btn_styles, 0)
        
        if style == 0: style = wx.OK|wx.CANCEL|wx.CENTER
        
        return style

    def create(self, meta_widget):
        self._setParams(meta_widget)

        return wx.MessageDialog(**self.params)